import { hashHistory } from 'dva/router';

export default {
    namespace: 'map',
    state: {
        mapPoints: [{
            id:'p00',
            longitude:116.468021,
            latitude:39.890092,
            markerContent: '<div>111a<div>'
        },{
            id:'p01',
            longitude:117.468021,
            latitude:39.890092,
            canShowLabel: true,
            config: {
                labelContent: '1dfasdf',
                isAnimation: true,
                // BAnimationType: 0
            }
        },{
            id: 'car1',
            longitude:116.385827,
            latitude:39.913232
        }],
        mapLines: [{
            id: 'l1',
            paths: [[116.468021,39.890092],[117.468021,38.890092]],
            config: {
                // isHidden: true
            }
        },{
            id: 'l2',
            paths: [[118.468021,37.890092],[119.468021,38.890092]],
            config: {lineWidth:4,color:'#fff'}
        }],
        mapPolygons:[{
            id: 'm1',
            rings: [[115.468021,38.890092],[117.468021,37.890092],[118.468021,39.890092]]
        },{
            id: 'm2',
            rings: [[116.468021,38.890092],[117.468021,39.890092],[118.468021,37.890092]]
        }],
        mapCircles:[{
            id: 'c1',
            longitude:116.468021,
            latitude:39.890092,
            radius: 10000,
        },{
            id: 'c2',
            longitude:117.468021,
            latitude:38.890092,
            radius: 10000,
        },],
        mapDraw: {},
        isDraw: false,
        isClearAll: false,
        mapRemove: [],
        isRemove: false,
        isCloseDraw: false,
        drawLinePoints: {}
    },
    subscriptions:{
        setup({ dispatch, history }) {
            history.listen(location => {
            })
        }
    },
    effects:{
        *drawPolyline({payload},{select,put,call}){
            let {drawLinePoints} = yield select(({map})=>map);
            let mapDraw = {
                geometryType: 'polyline',
                parameter: {},
                data: {id: 'polyline-test'}
            };
            yield put({
                type: 'updateState',
                payload: {
                    mapDraw,
                    isDraw: true,
                    drawLinePoints: {
                        ...drawLinePoints,
                        [mapDraw.data.id]: []
                    }
                }
            });
            yield call(delay,1);
            yield put({
                type: 'updateState',
                payload: {
                    isDraw: false
                }
            })
        },
        *removeGraphic({payload},{select,put,call}){
            let {drawLinePoints} = yield select(({map})=>map);
            let mapRemove = [{
                id: 'polyline-test',
                type: 'draw'      
            }];
            let dlps = {
                ...drawLinePoints
            }
            mapRemove.map((item,index)=>{
                dlps[item.id] = [];
            })
            yield put({
                type: 'updateState',
                payload: {
                    mapRemove,
                    isRemove: true,
                    drawLinePoints: dlps
                }
            });
            yield call(delay,1);
            yield put({
                type: 'updateState',
                payload: {
                    isRemove: false
                }
            })
        },
        *clearAll({payload},{select,put,call}){
            yield put({
                type:'updateState',
                payload:{
                    isClearAll: true,
                    mapPoints: [],
                    mapLines: [],
                    mapPolygons: [],
                    mapCircles: [],
                    boundaryName: [],
                    drawLinePoints: {}
                }
            });
            yield call(delay,1);
             yield put({
                type:'updateState',
                payload:{
                    isClearAll: false,
                }
            });
        },
    },
    reducers:{
        updateState(state,action){
            return {...state,...action.payload};
        }
    }
}

//延迟
function delay(timeout){
  var pro = new Promise(function(resolve,reject){
    setTimeout(resolve, timeout);
  });
  return pro;
}